﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helper.Business.Implementation;
using Helper.Core.Entity;
using Helper.Infrastructure.Session;
using System.Reflection;
using Helper.Infrastructure.Extensions;
using Helper.Business.Repository;

namespace Helper.Controllers
{
    public partial class CallController : BaseController
    {

        ICallRepository _CallRepository;

        public CallController() 
        {
            if (_CallRepository == null)
                _CallRepository = new CallRepository();

            this.LoginNeeded = true;
        }

        //
        // GET: /Call/

        [HttpPost]
        public ActionResult Index(Helper.Metadata.FilterCallMetadata Filters)
        {
            if (Filters != null)
            {
                Helper.Infrastructure.Call.FilterCall filterClass = new Helper.Infrastructure.Call.FilterCall(Filters.Username, Filters.Businessname, Filters.InitialDate, Filters.FinalDate);
                return View(_CallRepository.GetAllByFilters(filterClass, null));
            }
            else
            {
                return View(_CallRepository.GetAll());
            }
        }

        [HttpGet]
        public ActionResult Index(string Field, string OrderProperties)
        {
            User userLogged = new UserRepository().FindById(UserSession.GetInstance.Id);
            return View(_CallRepository.GetAllUserRelated(userLogged));
        }

        //
        // GET: /Call/Details/5

        public ActionResult Details(int id)
        {
            ViewBag.Title = string.Format("{0} - Detalhes", id);
            Call call = _CallRepository.FindById(id);
            call.Posts = new PostRepository().GetAllByCall(id);
            ViewBag.StatusList = new StatusRepository().GetAll();
            return View(call);
        }

        [HttpPost]
        public ActionResult Details(int id, Post post)
        {
            Call parentCall = new Call();
            post.ParentCall = _CallRepository.FindById(id);
            post.Author = new UserRepository().FindById(UserSession.GetInstance.Id);
            post.Timestamp = DateTime.Now;
            new PostRepository().Add(post);
            return Details(id);
        }

        //
        // GET: /Call/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Call/Create

        [HttpPost]
        public ActionResult Create(Call collection)
        {
            try
            {
                // TODO: Add insert logic here
                Call newCall = new Call();
                newCall = collection;
                newCall.Author = new UserRepository().FindById(UserSession.GetInstance.Id);
                newCall.Timestamp = DateTime.Now;
                _CallRepository.Add(newCall);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Call/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_CallRepository.FindById(id));
        }

        //
        // POST: /Call/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Call collection)
        {
            try
            {
                // TODO: Add update logic here
                _CallRepository.Save(collection);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Call/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Call/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult Search(bool SearchCalls = false)
        {
            ViewBag.SearchCalls = SearchCalls;
            return View(_CallRepository.GetAll());
        }
    }
}
